package com.fqcar.fqdf.ReportingProblems;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.afollestad.materialdialogs.MaterialDialog;
import com.fqcar.fqdf.R;
import com.fqdf.framework.android.adapter.BaseRecycleAdapter;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 取消原因的adapter
 */
public class ReportingProblemsTextAdapter extends BaseRecycleAdapter<String, ReportingProblemsTextAdapter.ReasonHolder> {

    private int checkPosition = -1;

    private MaterialDialog reasonDialog;

    private int dialogCheckPosition = -1;

    private ReportingProblemsActivity mContext;

    public ReportingProblemsTextAdapter(ReportingProblemsActivity mContext) {
        super(mContext);
        this.mContext = mContext;
        setList(Arrays.asList(mContext.getResources().getStringArray(R.array.reason_normal_new)));
    }


    @Override
    public ReportingProblemsTextAdapter.ReasonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ReportingProblemsTextAdapter.ReasonHolder(getLayoutInflater().inflate(R.layout.activity_report_problems_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ReportingProblemsTextAdapter.ReasonHolder holder, final int position) {

        final String reason = getItem(position);
        holder.cancelReasonTxt.setText(reason);

        holder.cancelReasonTxt.setChecked(checkPosition == position);

        holder.cancelReasonTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                /*if (position == getItemCount() - 1) {
                    getShowReasonDialog().show();
                } else {
                    checkPosition = position;
                    dialogCheckPosition = -1;
                }*/
                checkPosition = position;
                dialogCheckPosition = -1;
                notifyDataSetChanged();
                mContext.setBackground();
            }
        });
    }

    /**
     * 获取选择更多原因的dialog
     *
     * @return
     */
    private MaterialDialog getShowReasonDialog() {
        if (reasonDialog == null) {
            reasonDialog = new MaterialDialog.Builder(getContext())
                    .title(R.string.more_reason_choose)
                    .titleColorRes(R.color.c)
                    .items(R.array.reason_more)
                    .choiceWidgetColor(getContext().getResources().getColorStateList(R.color.a))
                    .itemsCallbackSingleChoice(
                            -1, new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                    checkPosition = getItemCount() - 1;
                                    dialogCheckPosition = which;
                                    notifyDataSetChanged();
                                    return true;
                                }
                            })
                    .build();
        } else {
            reasonDialog.setSelectedIndex(dialogCheckPosition);
        }


        return reasonDialog;
    }

    /**
     * 获取当前选中的原因
     *
     * @return
     */
    public String getReasonText() {
        if (checkPosition < 0) {
            return null;
        } /*else if (checkPosition == getItemCount() - 1) {
            return reasonDialog.getItems().get(dialogCheckPosition).toString();
        }*/ else {
            return getItem(checkPosition);
        }
    }

    public static class ReasonHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cancel_reason_txt)
        CheckBox cancelReasonTxt;

        ReasonHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
